package com.cgx;

import java.util.*;

/**
 * @author chenguanxin
 * @date 2020-12-16 21:04
 */
public class GetDifferent {

    public static void main(String[] args) {
        List<Map<String, String>> oldList = new ArrayList<>();
        List<Map<String, String>> newList = new ArrayList<>();
        long start = System.currentTimeMillis();
        Random random = new Random(System.currentTimeMillis());
        for (int i = 0; i < 1000; i++) {
            int value = random.nextInt(2000);
            Map<String, String> map = new HashMap<>();
            map.put("addr1", value + "");
            map.put("addr2", value + "");
            if (i % 2 == 0) {
                newList.add(map);
            } else {
                oldList.add(map);
            }
        }
        removeDuplication(oldList);
        removeDuplication(newList);
        getDifferent(oldList, newList);
//        classicGetDifferent(oldList, newList);
        long end = System.currentTimeMillis();
        System.out.println("总耗时: " + (end - start));
    }

    private static void removeDuplication(List<Map<String, String>> list) {
        Set<String> set = new HashSet<>();
        Iterator<Map<String, String>> iterator = list.iterator();
        while (iterator.hasNext()) {
            Map<String, String> map = iterator.next();
            String str = map.get("addr1") + "_" + map.get("addr2");
            if (set.contains(str)) {
                iterator.remove();
            } else {
                set.add(str);
            }
        }
    }

    //  100总耗时: 4
    //  1000总耗时: 12
    //  10000总耗时: 67
    //  100000总耗时: 485
    private static void getDifferent(List<Map<String, String>> oldList, List<Map<String, String>> newList) {
        Map<String, Object> oldMap = new HashMap<>();
        Map<String, Object> newMap = new HashMap<>();

        List<Map<String, String>> moreList = new ArrayList<>();
        List<Map<String, String>> lessList = new ArrayList<>();

        for (Map<String, String> oldNode: oldList){
            oldMap.put(oldNode.get("addr1") + "_" + oldNode.get("addr2"), oldNode);
        }

        for (Map<String, String> newNode: newList){
            newMap.put(newNode.get("addr1") + "_" + newNode.get("addr2"), newNode);
        }

        Set<String> newSet = newMap.keySet();
        Set<String> oldSet = oldMap.keySet();

        //  找出newList 比oldList 多的数据
        for (String newKey : newSet) {
            if (!oldSet.contains(newKey)) {
                moreList.add((Map<String, String>) newMap.get(newKey));
            }
        }
        //  找出newList 比oldList 少的数据
        for (String oldKey : oldSet) {
            if (!newSet.contains(oldKey)) {
                lessList.add((Map<String, String>) oldMap.get(oldKey));
            }
        }

        System.out.println("newList: " + newList.toString());
        System.out.println("oldList: " + oldList.toString());
        System.out.println("moreList: " + moreList.toString());
        System.out.println("lessList: " + lessList.toString());
    }


    //  100总耗时: 7
    //  1000总耗时: 152
    //  10000总耗时: 6372
    //  100000总耗时: 未知
    private static void classicGetDifferent(List<Map<String, String>> oldList, List<Map<String, String>> newList) {
        List<Map<String, String>> moreList = new ArrayList<>();
        List<Map<String, String>> lessList = new ArrayList<>();

        //  找出newList 比oldList 多的数据
        for (Map<String, String> newMap : newList) {
            for (int i = 0; i < oldList.size(); i++) {
                Map<String, String> oldMap = oldList.get(i);
                if ((newMap.get("addr1") + "_" + newMap.get("addr2")).equals(oldMap.get("addr1") + "_" + oldMap.get("addr2"))) {
                    break;
                } else if (i == oldList.size() - 1){
                    moreList.add(newMap);
                }
            }

        }

        //  找出newList 比oldList 少的数据
        for (Map<String, String> oldMap : oldList) {
            for (int i = 0; i < newList.size(); i++) {
                Map<String, String> newMap = newList.get(i);
                if ((newMap.get("addr1") + "_" + newMap.get("addr2")).equals(oldMap.get("addr1") + "_" + oldMap.get("addr2"))) {
                    break;
                } else if (i == oldList.size() - 1){
                    lessList.add(oldMap);
                }
            }
        }

        System.out.println("newList: " + newList.toString());
        System.out.println("oldList: " + oldList.toString());
        System.out.println("moreList: " + moreList.toString());
        System.out.println("lessList: " + lessList.toString());
    }
}
